﻿/// <summary>
/// Body mea mgr imp.  Since  2017 06 01  By Shawn Pan
/// In this script we develop some new way to measure body
/// Within these ways, we have the func get the pix pos of head top
///  The func we make a smallest rect which contains all the pixels  of  the  pixels of center  player contour. 
/// </summary>



using System.Collections;
using UnityEngine;
using System;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using IMIForUnity;


namespace IMIForUnity
{
	
	public  partial class BodyMeaMgrImp
	{

		//The following paras are for the frame paras of the whole player contour.
		public 	int MinContourLimitX=639; //To set this val to big. it easy for use to check that if one val is smaller than it, replace it with smaller on.
		public	int MaxContourLimitX=0;   //To set this val to smallest.  like the upper one.
		public	int BottomContourLimitY=0;	
		public	int MiddleContourLimitY=0;
	
		//headTopPixPos is in position BodyMeaMgrImp.cs  row  803.
	
		public	int stepX=10;  //we scan a span of 10 pixels in X dire each time to reach the boundary of the contour.
		public	int chestdownX=0;
		public	int chestdownY=0;
		public	int HipsY=0;
		public	int WaistY=0;

		public int LeftChestLimit;
		public int RightChestLimit;

		public int LeftWaistLimit;
		public int RightWaistLimit;

		public int LeftHipsLimit;
		public int RightHipsLimit;



		float	remainGirthRatioChest0=1.19f;
		float	remainGirthRatioChest1=0.796f;

		float	remainGirthRatioWaist0=0.93f;
		float	remainGirthRatioWaist1=0.99f;
		
		float	remainGirthRatioHips0=0.64f;
		float	remainGirthRatioHips1=1.31f;
		



		//We need to get headtopPix pos as an input. So we must get the headtop necessariely before this func
		public void FrameCenterPlayer(int controlId, Vector3 headTopPixPos, int kneePixPoxY,  bool isDrawOnDepthMap)
		{
			if(headTopPixPos.y<0) headTopPixPos.y=0;
			if(headTopPixPos.y>DEPTHHEIGHT-1)  headTopPixPos.y = DEPTHHEIGHT-1;

			if (depthPlayerDataOriginal != null) {
					
					int centerPixX = 0;
					int centerPixY = 0;
					
					
					if (DEPTHWIDTH > 0) {
						centerPixX = DEPTHWIDTH / 2;
					} else {
						DEPTHWIDTH = 640;
						centerPixX = 320;
					}
					
					
					//	depthPlayerDataOriginal  [chestupy *dw +chestupx] & 7
					
					
					
					if (DEPTHHEIGHT > 0) {
						centerPixY = (DEPTHHEIGHT / 3)*2;
					} else {	
						DEPTHHEIGHT = 480;
						centerPixY = 320;
					}
					
					

				int beginMeaX=0;
				bool findCenterPlayer=false;
				for (int i = centerPixX - 40; i < centerPixX + 40; i++) {
						
						if ((depthPlayerDataOriginal [centerPixY * DEPTHWIDTH + i] & 7) > 0) {
							
							currInsidePlayerPixID= (depthPlayerDataOriginal [centerPixY * DEPTHWIDTH + i] & 7);
						    beginMeaX=i;
						    findCenterPlayer=true;
					     	break;
						}
					}
					
				 MinContourLimitX=639; //To set this val to big. it easy for use to check that if one val is smaller than it, replace it with smaller on.
				 MaxContourLimitX=0;   //To set this val to smallest.  like the upper one.
				 BottomContourLimitY=0;
				 stepX=10;  //we scan a span of 10 pixels in X dire each time to reach the boundary of the contour.

				if(findCenterPlayer)
				{
					//loop , find the bottom contour, the lowest of the player pix contours;
					for(int i=centerPixY;i<DEPTHHEIGHT;i++)
					{
						bool foundCenterPlayerIDinWholeRow=false;
						for(int j=0;j<DEPTHWIDTH;j++)
						{
							if( (depthPlayerDataOriginal [i * DEPTHWIDTH + j] & 7)== currInsidePlayerPixID)
							{
								foundCenterPlayerIDinWholeRow=true;
								break;
							}
						}
						if( foundCenterPlayerIDinWholeRow)
						{
							continue;
						}
						else
						{
							BottomContourLimitY=i-1;
							if(BottomContourLimitY<1){BottomContourLimitY=1;}
							if(BottomContourLimitY>DEPTHHEIGHT-1){BottomContourLimitY=DEPTHHEIGHT-1;}
							break;
						}

					}


		#region  locate the left and right boundary of the player contour.
					//loop 1, find on quater to find the left min x boundary of the contour.  and the right boundary of the contour
					//int minX=0;
					for(int i=(int)(headTopPixPos.y);i<kneePixPoxY;i++)
					{
						bool findLimitMin=false;
						int tmpBeginX0=beginMeaX;
						while(!findLimitMin)
						{
							bool flag0=false; //If we found one player id in 10 pixles, which means we do not find the x boundary, we set this true;
							int mm=0;
							if((tmpBeginX0-stepX>0)&&(tmpBeginX0-stepX<DEPTHWIDTH))
							{
								mm=tmpBeginX0-stepX;
							}

								for(int j=mm;j<tmpBeginX0;j++)
								{
									try{
										if(currInsidePlayerPixID==(depthPlayerDataOriginal [i * DEPTHWIDTH + j] & 7))
										{
											flag0=true;
											tmpBeginX0=j;
											break;
										}
									}
									catch(System.IndexOutOfRangeException e)
									{
										Debug.LogError( "Get E in traversing body contour: "+i.ToString()+"  "+j.ToString());
									}

								}
														

							if(!flag0)
							{
								findLimitMin=true;
							}
						}

						if(tmpBeginX0<MinContourLimitX)
						{
							MinContourLimitX=tmpBeginX0;
						}




						bool findLimitMax=false;
						int tmpBeginX1=beginMeaX;
						while(!findLimitMax)
						{
							bool flag0=false; //If we found one player id in 10 pixles, which means we do not find the x boundary, we set this true;
							int mm=0;
							if(tmpBeginX1+stepX<DEPTHWIDTH-1)
							{
								mm=tmpBeginX1+stepX;
							}
							
							for(int j=mm;j>tmpBeginX1;j--)
							{
								if(currInsidePlayerPixID==(depthPlayerDataOriginal [i * DEPTHWIDTH + j] & 7))
								{
									flag0=true;
									tmpBeginX1=j;
									break;
								}
							}
							if(!flag0)
							{
								findLimitMax=true;
							}
						}
						
						if(tmpBeginX1>MaxContourLimitX)
						{
							MaxContourLimitX=tmpBeginX1;
						}
				 }
		#endregion

					MiddleContourLimitY= (BottomContourLimitY+(int)(headTopPixPos.y))/2;


					if(isDrawOnDepthMap)
					{
//						DrawingSpotsOnTexture.DrawPixelsSquareOnByteArray(DEPTHWIDTH,DEPTHHEIGHT,
//						                                                  beginMeaX,BottomContourLimitY,
//						                                                  5,allUsersDepthData,0,255,0,255);	
//
						if(headTopPixPos.y<0)headTopPixPos.y=0;
						if(headTopPixPos.y>DEPTHHEIGHT-1)headTopPixPos.y=DEPTHHEIGHT-1;
						
						if(BottomContourLimitY<0)BottomContourLimitY=0;
						if(BottomContourLimitY>DEPTHHEIGHT-1)BottomContourLimitY=DEPTHHEIGHT-1;


						if(MinContourLimitX<0)MinContourLimitX=0;
						if(MinContourLimitX>DEPTHWIDTH-1)MinContourLimitX=DEPTHWIDTH-1;

						if(MaxContourLimitX<0)MaxContourLimitX=0;
						if(MaxContourLimitX>DEPTHWIDTH-1)MaxContourLimitX=DEPTHWIDTH-1;
						//Draw square around the contour.
						for(int i=(int)(headTopPixPos.y);i<kneePixPoxY;i++)
						{
							DrawingSpotsOnTexture.DrawPixelsSquareOnByteArray(DEPTHWIDTH,DEPTHHEIGHT,
							                                                  MinContourLimitX,i,
							                                                  3,allUsersDepthData,0,255,0,255);
							DrawingSpotsOnTexture.DrawPixelsSquareOnByteArray(DEPTHWIDTH,DEPTHHEIGHT,
							                                                  MaxContourLimitX,i,
							                                                  3,allUsersDepthData,0,255,0,255);
						}



//						for(int i=MinContourLimitX;i<MaxContourLimitX;i++)
//						{
//							DrawingSpotsOnTexture.DrawPixelsSquareOnByteArray(DEPTHWIDTH,DEPTHHEIGHT,
//							                                                  i,(int)(headTopPixPos.y),
//							                                                  3,allUsersDepthData,0,255,0,255);
//							DrawingSpotsOnTexture.DrawPixelsSquareOnByteArray(DEPTHWIDTH,DEPTHHEIGHT,
//							                                                  i,BottomContourLimitY,
//							                                                  3,allUsersDepthData,0,255,0,255);
//
//							DrawingSpotsOnTexture.DrawPixelsSquareOnByteArray(DEPTHWIDTH,DEPTHHEIGHT,
//							                                                  i,MiddleContourLimitY,
//							                                                  3,allUsersDepthData,0,255,0,255);
//
//						}

					}

				}
		
			}
					
		}




	}


}